package com.woniu.darryringmall.ucenter.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniu.darryringmall.front.entity.Comment;
import com.woniu.darryringmall.front.entity.CommentVo;
import com.woniu.darryringmall.front.entity.Order;
import com.woniu.darryringmall.front.entity.User;
import com.woniu.darryringmall.front.service.CommentService;
import com.woniu.darryringmall.utils.ResponseData;
import com.woniu.darryringmall.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("ucenter/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 获取未评价订单，并跳转个人中心我的评价页面
     */
    @PostMapping("list")
    @ResponseBody
    public ResponseData list( HttpSession session){
        List<CommentVo> Comments=commentService.getNoCmByUserId(((User)session.getAttribute("user")).getId());
        return new ResponseData(Comments);
    }

    /**
     * 获取已评价订单
     */
    @ResponseBody
    @PostMapping("commentOrder")
    public ResponseData commentOrder(HttpSession session){
        List<CommentVo> commentVos=commentService.getCmByUserId(((User)session.getAttribute("user")).getId());
        return new ResponseData(commentVos);
    }


    /**
     * 增加评价
     */
    @ResponseBody
    @PostMapping("add")
    public ResponseData add(int goodsId,int orderId,String context,String type,HttpSession session){
        User loginUser=(User) session.getAttribute("user");
        Comment comment=new Comment();
        comment.setUserId(loginUser.getId());
        comment.setGoodsId(goodsId);
        comment.setOrderId(orderId);
        comment.setContext(context);
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        comment.setTime(df.format(day));
        comment.setType(type);
        commentService.add(comment);
        return ResponseUtils.SUCCESS;
    }



    /**
     * 修改评价
     *
     */
    @ResponseBody
    @PostMapping("update")
    public ResponseData update(int id,String context,String type){
        UpdateWrapper<Comment> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("context",context);
        updateWrapper.set("type",type);
        commentService.update(updateWrapper);
        return ResponseUtils.SUCCESS;
    }

    /**
     * 删除评价
     */
    @ResponseBody
    @PostMapping("delete")
    public ResponseData delete(int id){
        commentService.del(id);
        return ResponseUtils.SUCCESS;
    }
}
